/*
 * ====================================================================================
 * Copyright 2012 Eric Hartwell  (http://www.ehartwell.com)
 * ====================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable
 * law or agreed to in writing, software distributed under the License is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 * ====================================================================================
 */

package com.ehartwell.samples.android.pagerview;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Adapter for fragment pager.
 */
public class PagerFragmentAdapter extends android.support.v4.app.FragmentPagerAdapter {
    private List<Fragment>  mViewFragments = new ArrayList<Fragment>();
    private int             mPrimaryItem = -1;
    private TextView        mTitleView;

    /**
     * Creates a new PagerFragmentAdapter.
     *
     * @param fm The fragment manager.
     * @param titleView TextView used for displaying page title, or <tt>null</tt> if not used.
     */
    public PagerFragmentAdapter(FragmentManager fm, TextView titleView) {
        super(fm);
        mTitleView = titleView;
    }

    /**
     * Adds a page fragment.
     *
     * @param position Position in list.
     * @param fragment Page fragment to add.
     */
    public void addFragment(int position, Fragment fragment) {
        mViewFragments.add(position, fragment);
    }

    /**
     * Removes a page fragment.
     *
     * @param fragment Page fragment to remove.
     */
    public void removeFragment(Fragment fragment) {
        mViewFragments.remove(fragment);
    }

    @Override
    /**
     * Gets the number of page fragments.
     *
     * @return Number of page fragments.
     */
    public int getCount() {
        return mViewFragments.size();
    }

    @Override
    /**
     * Gets a specific page fragment.
     *
     * @param position Index of requested fragment.
     * @return Page fragment at the specified position, or <tt>null</tt> if none there.
     */
    public Fragment getItem(int position) {
        return (position >= 0) && (position < mViewFragments.size()) ? mViewFragments.get(position) : null;
    }

    /**
     * Gets the primary (visible) item.
     *
     * @return Primary (visible) item, or <tt>null</tt> if none.
     */
    public Fragment getPrimaryItem() {
        return getItem(mPrimaryItem);
    }

    @Override
    /**
     * Called to inform the adapter of which item is currently considered to
     * be the "primary", that is the one show to the user as the current page.
     *
     * @param container The containing View from which the page will be removed.
     * @param position The page position that is now the primary.
     * @param object The same object that was returned by instantiateItem(View, int).
     */
    public void setPrimaryItem(ViewGroup viewGroup, int i, Object o) {
        super.setPrimaryItem(viewGroup, i, o);

        // Call the onPause() method of the old fragment and onResume() for the new one
        if (i != mPrimaryItem) {
            if (mPrimaryItem < 0) {
                // Deactivate them all the first time through
                for (Fragment fragment : mViewFragments) {
                    if (fragment instanceof PagerFragment) ((PagerFragment)fragment).onPauseFragment(viewGroup.getContext());
                }
            }
            else {
                // Deactivate the one we're leaving
                if (getItem(mPrimaryItem) instanceof PagerFragment) ((PagerFragment)getItem(mPrimaryItem)).onPauseFragment(viewGroup.getContext());
            }

            // Set the title
            if (mTitleView != null) {
                if (getItem(i) instanceof PagerFragment) {
                    ((PagerFragment)getItem(i)).onResumeFragment(viewGroup.getContext());
                    mTitleView.setText(((PagerFragment)getItem(i)).getTitle());
                }
                else {
                    mTitleView.setText("");
                }
            }

            // Remember the active fragment
            mPrimaryItem = i;
        }
    }
}
